//Ryan Clark
//Array project
//c1
import java.util.Scanner;
import java.io.*;
public class Array {
    public static void main(String[] args) throws FileNotFoundException, Exception{
    

    //initialize arrays
    String[] lastName = new String[254];
    String[] firstName = new String[254];
    String[] id = new String[254];
    String[] graduationStatus = new String[254];
    double[] gpa = new double[254];
    int[] creditHours = new int[254];
    String[] gradePointAverage = new String[254];
    String[] credits = new String[254]; 

    System.out.println(lastName[0]);
    //make arrays
    ArrayMaker(lastName,
    firstName, id, graduationStatus,
    gpa, creditHours, gradePointAverage, credits);

    //sort arrays
    ArraySort(lastName,
    firstName, id, graduationStatus,
    gpa, creditHours, gradePointAverage, credits);

    printData(lastName,
    firstName, id, graduationStatus,
    gpa, creditHours, gradePointAverage, credits);
    
    
    }//end void main
    
    public static void ArrayMaker(String[] lastName,
    String[] firstName,String[] id, String[] graduationStatus,
    double[] gpa, int[] creditHours,String[] gradePointAverage,
    String[] credits) throws FileNotFoundException
    {//start void 1

    //intialize file path and scanner reader
    File pathInput = new File("I:\\Graduation.txt");
    Scanner input = new Scanner(pathInput);

    //intialize array indexes
    int creditHoursIndex = 6;
    int arrayIndex = 3;
    int gpaIndex = 7;

    //intialize boolean control variables
    boolean gpaCorrect, creditHoursCorrect;
    String last;
    String grad;
    boolean[] boolArray = {true,true,false,true,true,false,false,false};
    String[] messageArray = {"can graduate","Credit hours too low",
    "GPA too low","GPA and credit hours too low"};
   
    int counter = 0;
    while(input.hasNext()){//end whie l
    creditHoursIndex = 6;
    gpaIndex = 7;
    arrayIndex = 3;

    //read in input
    last = input.next();
    firstName[counter] = input.next();
    creditHours[counter] = input.nextInt();
    gpa[counter] = input.nextDouble();
    gradePointAverage[counter] = Double.toString(gpa[counter]);
    credits[counter]= Integer.toString(creditHours[counter]);

    //assign id and last name
    id[counter] = last.substring(0, 4);
    lastName[counter] = last.substring(4, last.length()-1);

    //create loop conditions
    gpaCorrect = gpa[counter] > 2.0;
    creditHoursCorrect = creditHours[counter] >= 64;

    //assign gradaution message
    while(gpaCorrect != boolArray[gpaIndex] || creditHoursCorrect !=
            boolArray[creditHoursIndex]){//while 2
        gpaIndex = gpaIndex - 2;
        creditHoursIndex = gpaIndex- 1;
        arrayIndex--;
    }//end while 2
    grad = messageArray[arrayIndex];
    graduationStatus[counter] = grad;
    counter++;
    }//end while 1
    input.close();
    }//end void 1

    public static void ArraySort(String[] lastName,
    String[] firstName,String[] id, String[] graduationStatus,
    double[] gpa, int[] creditHours,String[] gradePointAverage,
    String[] credits)
    {//begin void 2

    //loop control variable


    String temp;

    for(int i = 0; i  < lastName.length - 2; i++){//start for 1
               for(int j=0; j < (lastName.length -2); j++)
         {//start for 2
         

         if (lastName[j+1].compareTo(lastName[j])<0)
         {//start if 1
            temp = lastName[j];
            lastName[j] = lastName[j+1];
            lastName[j + 1] = temp;

            temp = firstName[j];
            firstName[j] = firstName[j+1];
            firstName[j + 1] = temp;

            temp = id[j];
            id[j] = id[j+1];
            id[j + 1] = temp;

            temp = graduationStatus[j];
            graduationStatus[j] = graduationStatus[j+1];
            graduationStatus[j + 1] = temp;

            temp = gradePointAverage[j];
            gradePointAverage[j] = gradePointAverage[j+1];
            gradePointAverage[j + 1] = temp;

            temp = credits[j];
            credits[j] = credits[j+1];
            credits[j + 1] = temp;

         }//end if 1

         //do this if lastName cannot be compared
         if(lastName[j].equals(lastName[j+1]) &&
                 (!firstName[j].equals(firstName[j+1])) ){//start if 2

         //compare firstName if lastName fails
         if(firstName[j+1].compareTo(firstName[j])<0)
         {//start if 3
            temp = lastName[j];
            lastName[j] = lastName[j+1];
            lastName[j + 1] = temp;

            temp = firstName[j];
            firstName[j] = firstName[j+1];
            firstName[j + 1] = temp;

            temp = id[j];
            id[j] = id[j+1];
            id[j + 1] = temp;

            temp = graduationStatus[j];
            graduationStatus[j] = graduationStatus[j+1];
            graduationStatus[j + 1] = temp;

            temp = gradePointAverage[j];
            gradePointAverage[j] = gradePointAverage[j+1];
            gradePointAverage[j + 1] = temp;

            temp = credits[j];
            credits[j] = credits[j+1];
            credits[j + 1] = temp;
         }//end if  3

         }//end if 2
        }//end for 2
        }//end for 1
 
    int counter = 0;
        while(counter< 253){
    System.out.println(id[counter] + " " + lastName[counter] + ", " +
    firstName[counter] + " " + gpa[counter] + " " + creditHours[counter] + " "
    + graduationStatus[counter]);
    counter++;
             }
    }//end void 2

    public static void printData(String[] lastName,
    String[] firstName,String[] id, String[] graduationStatus,
    double[] gpa, int[] creditHours,String[] gradePointAverage,
    String[] credits)throws Exception{//void 3

    //initialize file path
    File pathOutput = new File("I:\\OutputGraduation.txt");

    //initialize printwriter to write data
    PrintWriter output = new PrintWriter(pathOutput);

    //print arrays
    int counter = 0;
    while(counter< 253){//while a
    if(graduationStatus[counter].equals("can graduate")){//if a
    output.println(id[counter] + " " + lastName[counter] + ", " +
    firstName[counter] + " " + gpa[counter] + " " + creditHours[counter] + " "
    + graduationStatus[counter]);
    }//end if a
    counter++;
    }//end while a
    output.println("");
    output.println("");
    output.println("");

    counter = 0;
    while(counter< 253){//while b
    if(graduationStatus[counter].equals("Credit hours too low")){// if b
    output.println(id[counter] + " " + lastName[counter] + ", " +
    firstName[counter] + " " + gpa[counter] + " " + creditHours[counter] + " "
    + graduationStatus[counter]);
    }//end if b
    counter++;
    }//end while b

    output.println("");
    output.println("");
    output.println("");

    counter = 0;
    while(counter< 253){//while c
    if(graduationStatus[counter].equals("GPA too low")){// if c
    output.println(id[counter] + " " + lastName[counter] + ", " +
    firstName[counter] + " " + gpa[counter] + " " + creditHours[counter] + " "
    + graduationStatus[counter]);
    }//end if c
    counter++;
    }//end while c

    output.println("");
    output.println("");
    output.println("");

    counter = 0;
    while(counter< 253){//while d
    if(graduationStatus[counter].equals("GPA and credit hours too low")){// if d
    output.println(id[counter] + " " + lastName[counter] + ", " +
    firstName[counter] + " " + gpa[counter] + " " + creditHours[counter] + " "
    + graduationStatus[counter]);
    }//end if d
    counter++;
    }//end while d

    output.close();
    }//end void 3
}//end class Array
